import {Request} from 'express'
import { IMockDataItem } from './getMockData'
type matchMockRes = IMockDataItem| void
export default function matchMock  (req:Request, mockData: IMockDataItem[],):matchMockRes{
    const { path: targetPath, method } = req;
    const targetMethod = method.toLowerCase();

    for (const mock of mockData) {
        const { method, re, keys } = mock;
        if (method === targetMethod) {
          const match = re.exec(targetPath);
          if (match) {
            const params = {};
            for (let i = 1; i < match.length; i += 1) {
              const key = keys[i - 1];
              const prop = key.name;
              const val = decodeParam(match[i]);
              // @ts-ignore
              if (val !== undefined || !hasOwnProdperty.call(params, prop)) {
                params[prop] = val;
              }
            }
            req.params = params;
            return mock;
          }
        }
    }
}

function decodeParam(val: any) {
    if (typeof val !== 'string' || val.length === 0) {
      return val;
    }
    try {
      return decodeURIComponent(val);
    } catch (err) {
      if (err instanceof URIError) {
        err.message = `Failed to decode param ' ${val} '`;
        // @ts-ignore
        err.status = 400;
        // @ts-ignore
        err.statusCode = 400;
      }
      throw err;
    }
  }